<template>
  <div class="component-container">
    <div class="component-container__main">
      <el-form ref="dataFormRef" :rules="rules" :model="goodsInfo" label-width="120px">
        <!-- <el-form-item label="商品名称" prop="name">
          <el-input v-model="goodsInfo.name" style="width: 400px" />
        </el-form-item> -->

        <!-- <el-form-item label="商品品牌" prop="brandId">
          <el-select v-model="goodsInfo.brandId" style="width: 400px" clearable>
            <el-option
              v-for="item in brandOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item label="首页推荐栏目" prop="gender">
          <dictionary v-model="goodsInfo.indexId" type-code="index_type" />
        </el-form-item>

        <el-card style="max-width: 800px">
          <template #header>
            <div class="card-header">
              <span>商品购买方式配置</span>
            </div>
          </template>

          <el-table :data="points.configs" style="width: 100%">
            <el-table-column prop="typeName" label="购买方式" width="180">
              <template #default="{ row }">
                <el-checkbox v-model="row.checked" @change="handleCheckboxChange(row)">
                  {{ row.typeName }}
                </el-checkbox>
              </template>
            </el-table-column>

            <el-table-column label="参数" width="180">
              <template #default="{ row }">
                <el-input-number v-model="row.ratio" :min="0" :max="100" :step="0.1" controls-position="right"
                  style="width: 120px" />
              </template>
            </el-table-column>

            <el-table-column label="说明" prop="content">
              <template #default="{ row }">
                <div class="content-display">
                  {{ row.content || '暂无说明' }}
                </div>
              </template>
            </el-table-column>
          </el-table>
        </el-card>



        <!-- <el-form-item label="零售价" prop="originPrice">
          <el-input v-model="goodsInfo.originPrice" style="width: 400px" />
        </el-form-item>

        <el-form-item label="促销价" prop="price">
          <el-input v-model="goodsInfo.price" style="width: 400px" />
        </el-form-item> -->


      </el-form>
    </div>
    <div class="component-container__footer">
      <el-button @click="handlePrev">上一步，设置商品库存</el-button>
      <el-button type="primary" @click="submitForm">提交</el-button>

    </div>
  </div>
</template>

<script setup lang="ts">
import { addSpu, updateSpu } from "@/api/pms/goods";
import { useRouter } from "vue-router";
import { getPointsConfigDefault, getPointsConfig, addPointsConfig } from "@/api/points/config";
const router = useRouter();
const emit = defineEmits(["prev", "next", "update:modelValue"]);
const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {
      return {};
    },
  },
});
const goodsInfo: any = computed({
  get: () => props.modelValue,
  set: (value) => {
    emit("update:modelValue", value);
  },
});

const points = reactive({ configs: [] });

/**
 * 商品表单提交
 */
function submitForm() {
  goodsInfo.value.pointsConfigList = points.configs;
  const goodsId = goodsInfo.value.id;
  let submitsData = Object.assign({}, goodsInfo.value);
  console.log('-----------submitsData----------')
  console.log(submitsData)
  console.log('---------------------')

  if (goodsId) {
    // 编辑商品提交
    updateSpu(goodsId, submitsData).then(() => {
      router.push({ path: "/pms/goods" });
      ElNotification({
        title: "提示",
        message: "编辑商品成功",
        type: "success",
      });
    });
  } else {
    // 新增商品提交
    addSpu(submitsData).then(() => {
      router.push({ path: "/pms/goods" });
      ElNotification({
        title: "提示",
        message: "新增商品成功",
        type: "success",
      });
    });
  }
}
function handlePrev() {
  emit("prev");
}
function loadData() {
  const goodsId = goodsInfo.value.id;
  if (goodsId) {
    // 编辑商品提交
    getPointsConfig(goodsId).then(({ data }) => {
      points.configs = data.list;
      console.log('----------编辑商品提交-----------')
      console.log(points.configs)
      console.log('---------------------')
    });
  } else {
    // 新增商品提交
    getPointsConfigDefault().then(({ data }) => {
      points.configs = data.list;
      console.log('---------新增商品提交------------')
      console.log(points.configs)
      console.log('---------------------')
    });

  }
}
onMounted(() => {
  goodsInfo.indexId = 0;
  loadData();

});
function handleCheckboxChange(row) {
  console.log(`${row.typeName} 选中状态: ${row.checked}`);
}
</script>

<style scoped>
.card-header {
  font-size: 16px;
  font-weight: bold;
}

/* 调整表格行高 */
:deep(.el-table .el-table__cell) {
  padding: 12px 0;
}

/* 复选框对齐 */
:deep(.el-checkbox-group) {
  display: flex;
  align-items: center;
  height: 100%;
}
</style>
